<?php

class Registration Extends MY_Controller
{
	private $students = [];
	private $student_chooser = TRUE;
	private $_user_id = '';

	public function __construct()
	{
		parent::__construct();
		$this->page_auth->secure_page();
		$this->load->library('hs');
		$this->view_data['system_message'] = $this->_msg();
		$this->_user_id = $this->session->userdata('userid');
		$this->load->model(['M_parent','M_school_years']);
		$this->load->library('hs');
		$this->_get_students_ids();
		$this->_student_chooser();
		$this->view_data['current_url'] = $this->router->class.'/'.$this->router->method.'/';
		$this->load->helper(['grade','misc','parent']);
		$this->view_data['__page_title'] = "Registration";
		$this->is_open_reg = $this->view_data['is_open_reg'] = $this->M_school_years->is_sy_open_reg();
	}

	private function _get_students_ids()
	{
		$userid = $this->session->userdata('userid');
		$this->students = $this->M_parent->get_user_students($userid);
	}

	private function _student_chooser()
	{
		if($this->student_chooser)
		{
			$this->view_data['for_student_chooser']['raw'] = $this->students;
			$this->view_data['for_student_chooser']['student'] = $this->M_parent->students($this->_user_id);
		}
	}

	private function _check_if_student_valid($id)
	{
		$rs = $this->M_parent->students($this->_user_id);
		if($rs){
			$pass = false;
			foreach ($rs as $key => $v) {
				if($v->enrollment_id==$id){
					$pass = true;
					break;
				}
			}
			if(!$pass){
				show_error('Selected student is invalid.');	
			}
		}else{
			show_error('No siblings found for this parent login.');
		}
	}

	public function index($hash='')
	{
		$this->view_data['current_id'] = $hash;
	}

	public function enroll($hash='')
	{

		$this->view_data['current_id'] = $hash;
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$this->load->model(['M_enrollees','M_levels']);

			$e_id = $x[0];
			$p_id = $x[1];
			
			$this->_check_if_student_valid($e_id);
			$this->view_data['profile'] = $p = $this->M_enrollees->get_enrollee_prof($e_id);
			$this->view_data['levels'] = $this->M_levels->get_lesser_levels_array($p->level_id);
			$this->view_data['academic_year'] = $this->M_school_years->open_reg_sy();

			if($this->M_enrollees->check_duplicate($p_id))
			{
				$this->view_data['duplicate'] = TRUE;
			}else{
				$this->view_data['duplicate'] = FALSE;
				if($this->input->post()){
					$this->load->library('form_validation');
					$this->form_validation->set_rules('grade_level', 'Grade Level', 'required|trim');

					if($this->form_validation->run() !== FALSE){
						
						$this->load->model('M_enrollment');
						$rs = $this->M_enrollment->enroll($this->input->post(), $e_id, $p_id);
						$goto = $rs->status ? 'registration' : current_url();
						$this->_msg($rs->code, $rs->msg, $goto);
					}
				}
			}

		}else{
			show_404();
		}
	}
	
	public function register($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$id = $x[0];
			$year = $x[1].' - '.$x[2];
			
			$this->view_data['for_year'] = $year;
			$this->view_data['hash'] = $x;
			$this->view_data['year_id'] = $id;
			
			if($this->input->post('confirm_registration'))
			{
				$this->_msg('s','Registration confirmed. Choose a payment plan.','registration/choose_payment_plan/'.$hash);
			}
			
			
		}else{
			show_404();
		}
	}
	
	public function choose_payment_plan($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$id = $x[0];
			$year = $x[1].' - '.$x[2];
			
			$this->view_data['for_year'] = $year;
			$this->view_data['hash'] = $x;
			$this->view_data['year_id'] = $id;
			
			if($this->input->post('save_payment_plan'))
			{
				$this->_msg('s','Payment plan is set. Choose section and schedule.','registration/choose_section/'.$hash);
			}
		
		}else{
			show_404();
		}
	}
	
	
	
	public function choose_section($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$id = $x[0];
			$year = $x[1].' - '.$x[2];
			
			$this->view_data['for_year'] = $year;
			$this->view_data['hash'] = $x;
			$this->view_data['year_id'] = $id;
			
			if($this->input->post('save_payment_plan'))
			{
				$this->_msg('s','Payment plan is set. Choose section and schedule.','registration/choose_section/'.$hash);
			}
		
		}else{
			show_404();
		}
	}

	public function finalize()
	{
	
	
	
	
	
	}
}